/**
 * Created by Administrator on 2018/12/29.
 */
var table;
var parentCode;
$(function(){
    table = $("#userList");
    parentCode = locations.ParamString("parentCode");
    console.log("parentCode:"+parentCode);
    //initComponent();
    initGrid();
    queryUserFunction();
    /*deleteMould();
    editMould();
    mouldDetail();*/
    /*var pager = $('#dg').datagrid().datagrid('getPager');	// get the pager of datagrid
    pager.pagination({
        buttons:[{
            iconCls:'icon-search',
            handler:function(){
                alert('search');
            }
        },{
            iconCls:'icon-add',
            handler:function(){
                alert('add');
            }
        },{
            iconCls:'icon-edit',
            handler:function(){
                alert('edit');
            }
        }]
    });*/
});

function initComponent() {
    var curr_time = new Date();
    var strDate = curr_time.getFullYear() + "-";
    strDate += curr_time.getMonth() + 1 + "-";
    strDate += curr_time.getDate() + " 00:00:00";
    $("#startTime").datetimebox("setValue", strDate);
    $("#endTime").datetimebox("setValue", addDate(curr_time, 0) + " 23:59:59");
}

function initGrid(){
    var param = queryButtonLimit(parentCode);
    table.datagrid({
        iconCls : 'icon-ok',
        pageSize : 10,
        pageList : [ 10, 20, 40, 80 ],
        striped : true,
        collapsible : false,
        remoteSort : true,// 服务器端排序
        fitColumns : false,
        singleSelect : true,
        showFooter : true,
        columns : [[{
                field : 'id',
                checkbox : true,
                hidden:true
            }, {
                field : 'linkman',
                title : '账号',
                width : 150,
                align : 'center'
            }, {
                field : 'nickname',
                title : '昵称',
                width : 120,
                align : 'center'
            }, {
                field : 'roleName',
                title : '角色',
                width : 120,
                align : 'center'
            }, {
                field : 'departmentId',
                title : '部门',
                width : 120,
                align : 'center',
                formatter:function (value, row, index) {
                    if(value == 'bg') {
                        return "办公室";
                    } else if(value == 'ar') {
                        return "车间";
                    }
                }
            }, {
                field : 'phone',
                title : '手机号',
                width : 120,
                align : 'center'
            }, {
                field : 'created',
                title : '创建时间',
                width : 200,
                align : 'center',
                formatter:function (value, row, index) {
                    if(row.created == null) {
                        return "";
                    }
                    return dateFtt("yyyy-MM-dd hh:mm:ss",new Date(row.created));
                }
            }, {
                field : 'lastLoginTime',
                title : '上次登录时间',
                width : 200,
                align : 'center',
                formatter:function (value, row, index) {
                    if(row.lastLoginTime == null) {
                        return "";
                    }
                    return dateFtt("yyyy-MM-dd hh:mm:ss",new Date(row.lastLoginTime));
                }
            }
        ]],
        pagination : true,
        rownumbers : true,
        toolbar: param,
        onBeforeLoad : function(param) {
            var parameter = new Object();
            parameter.page = param.page;
            parameter.rows = param.rows;
            if($("#linkman").val()!="") parameter.linkman = $("#linkman").val();
            if($("#nickname").val()!="") parameter.nickname = $("#nickname").val();
            if($("#departmentId").val()!="") parameter.departmentId = $("#departmentId").val();
            if($("#startTime").val()!="") parameter.startTime = $("#startTime").val();
            if($("#endTime").val()!="") parameter.endTime = $("#endTime").val();
            var jsonstr = postSyncAjax("/user/selectUserByPage", parameter);
            //console.log(jsonstr);
            //console.log(jsonstr.data);
            var json = jsonstr.data;
            var dataGridData = new Object();
            dataGridData.rows = json.rows;
            dataGridData.total = json.total;
            table.datagrid('loadData', dataGridData);
        }
    });
}
// 查询和重置
var queryUserFunction = function () {
    $("#queryUserBtn").click(function () {
        table.datagrid("reload");
    });
    $("#restoreBtn").click(function () {
        $("form[name='queryForm']").form('reset');
        initComponent();
        table.datagrid("reload");
    });
}

//删除
var deleteMould = function(){
    $("#delete").click(function () {
        var rows = table.datagrid('getSelected');
        if (null == rows) {
            $.messager.alert("消息提示", "至少选择一条记录", "warning");
            return;
        } else {
            $.messager.confirm("确认对话框","确定要删除该模具信息？",function (r) {
                if(r){
                    var parameter = new Object();
                    parameter.id = rows.id;
                    var jsonstr = postSyncAjax("/deleteMould", parameter);
                    console.log(jsonstr);
                    if(jsonstr.success) {
                        //$.messager.alert("消息提示", "模具删除成功！", "warning");
                        //table.datagrid("reload");
                        showDialogWithFunc("模具删除成功("+jsonstr.msg+")！",function () {
                            table.datagrid("reload");
                         });
                    } else {
                        //$.messager.alert("消息提示", "模具删除成功！", "warning");
                        showDialogWithContent("模具删除失败："+jsonstr.msg);
                    }
                }
            });
        }
    });
}

// 修改用户信息
var editUser = function (uri) {
    var rows = table.datagrid('getSelected');
    if (null == rows) {
        $.messager.alert("消息提示", "至少选择一条记录", "warning");
        return;
    }
    var id = rows.id;
    showThePage(parentCode, uri,{
        url:uri + "/" + id
    });
}

// 模具详情
var mouldDetail = function () {
    $("#detail").click(function () {
        var rows = table.datagrid('getSelected');
        if (null == rows) {
            $.messager.alert("消息提示", "至少选择一条记录", "warning");
            return;
        } else {
            //top.$('#cur_id').val(rows.id);
            //console.log("ceshi:"+rows.id);
            removeTabs('sh-tabs','模具详情');
            openMessageZone('/mouldDetail/'+rows.id);
            //createTabs('sh-tabs','/mouldDetail/'+rows.id,'模具详情','menu4');
        }
    });
}